﻿Imports System.Data
Imports System.Data.SqlClient
Imports System.Net.Mail


Partial Class driver_default
    Inherits System.Web.UI.Page

    'dim connstring as string = "data source=sql2008.campus.jcu.edu ; initial catalog=service; user id=thunder48; password=thundertech169;"
    Dim driverinfo As driverInfo = New driverInfo()
    Dim conn As New SqlConnection(driverinfo.getConnectionString())
    Protected Sub loginButton_click(sender As Object, e As System.EventArgs) Handles loginButton.Click


        If (email.Text = "quizAdmin" And password.Text = "quizAdmin") Then
            Response.Redirect("QuizAdmin.aspx")
        Else
            ' dim conn1 as new sqlconnection
            Dim cmd As String
            Dim cmd1 As String
            Dim sql As SqlCommand = New SqlCommand
            Dim sql1 As SqlCommand = New SqlCommand
            Dim reader As SqlDataReader
            Dim reader1 As SqlDataReader
            ' Dim sqlConn As SqlConnection = driverinfo.getConnectionString()
            'conn.ConnectionString = driverinfo.getConnectionString()
            If conn.ConnectionString = Nothing Then
                failureLabel.Text = "The application failed to make a connection to the database, please contact the system adminstrator."
            Else
                conn.Open()

                'select username,password from [user] where username='nsmetana11' and password='nsmetana11'

                'try
                'cmd = "select username,password,firstname,lastname from [user] where username='" & username.text & "' and password='" & password.text & "'"
                ' catch
                'response.write("you don't exist.")
                'end try

                'add admin authority
                cmd1 = " select username from [user] where [username]='" & email.Text & "' and [password]='" & password.Text & "'"
                sql1.CommandText = cmd1
                sql1.Connection = conn
                reader1 = sql1.ExecuteReader()

                If reader1.HasRows Then
                    Try
                        reader1.Read()

                        Session("loggedin") = True
                        Response.Redirect("emulate.aspx")


                    Catch
                        failureLabel.Text = "fatal error."
                    End Try

                Else
                    reader1.Close()
                    cmd = "select id,password,first_name,last_name,email from driver where email='" & email.Text & "' and [password]='" & password.Text & "'"

                    sql.CommandText = cmd
                    sql.Connection = conn
                    reader = sql.ExecuteReader()

                    If reader.HasRows Then
                        'response.redirect("portal.aspx")
                        Try
                            reader.Read()
                            'session("username") = reader.getstring(0)
                            'session("password") = reader.getstring(1)
                            'session("firstname") = reader.getstring(2)
                            'session("lastname") = reader.getstring(3)

                            Session("driver_id") = reader.GetValue(0)
                            Session("first_name") = reader.GetString(2)
                            Session("last_name") = reader.GetString(3)
                            Session("email") = reader.GetString(4)
                            Session("semester") = getsemester()
                            Session("loggedin") = True

                            Dim pw As String = reader.GetString(1)
                            If pw = "cssa" Then
                                Response.Redirect("reset.aspx")
                            Else
                                If Session("referer") = Nothing Then
                                    Response.Redirect("portal.aspx")
                                Else
                                    Response.Redirect(Session("referer"))
                                End If

                            End If


                        Catch
                            failureLabel.Text = "fatal error."
                        End Try
                        'response.write("success!")
                        'while (reader.read)
                        'reader.read()
                        'response.write(reader.getstring(0) & reader.getstring(1))
                        'end while
                    Else
                        failureLabel.Text = "login / password does not exist."

                    End If
                End If

                'sql.parameters.clear()



                'response.write(reader.getstring(0))


                'if classname.hasrows then
                'do while results.read()
                'response.write(results.getstring(0))
                'activitylist.items.add(results(0))
                'activitylist.datasource = classname
                'activitylist.datatextfield = "classname"
                'activitylist.databind()
                'loop
                'end if

                conn.Close()
            End If
        End If



     


    End Sub


    Public Function getsemester() As Integer

        Dim semester_yearid As Integer

        Dim conn As New SqlConnection(driverinfo.getConnectionString())
        Dim cmd As String
        Dim sql As SqlCommand = New SqlCommand
        Dim reader As SqlDataReader
        sql.Connection = conn
        conn.Open()
        cmd = "select top (1)semester_yearid, semester, year, start_date, end_date from comserv_semester_year where  (start_date < { fn now() }) order by semester_yearid desc"
        sql.CommandText = cmd
        'sql.Connection = conn
        reader = sql.ExecuteReader()

        If reader.HasRows Then
            reader.Read()
            'semester_yearid = reader.getstring(0)
            semester_yearid = reader.GetValue(0)
            'semester_yearid = 12
        Else
            semester_yearid = 0
        End If


        conn.Close()

        Return semester_yearid
        'return 11
    End Function

  

    Protected Sub quizButton_Click(sender As Object, e As EventArgs) Handles quizButton.Click
        Response.Redirect("quizLogin.aspx")
    End Sub
End Class
